package com.baylor.server.business.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class OSBaseTool {
	private final static String ANDROID="Android";
	private final static String IPHONE="iPhone";
	private final static String PADHONE="iPad";
	private final static String WINDOWS="Windows";
	private final static String OTHER="其它";
	
	
	public String checkBrowse(String userAgent){
		
		if(regex(ANDROID, userAgent))return ANDROID;
		if(regex(IPHONE, userAgent))return IPHONE;
		if(regex(PADHONE, userAgent))return PADHONE;
		if(regex(WINDOWS, userAgent))return WINDOWS;
		return OTHER;
	}
	public boolean regex(String regex,String str){
		Pattern p =Pattern.compile(regex,Pattern.MULTILINE);
		Matcher m=p.matcher(str);
		return m.find();
	}
	
	public static void main(String[] args) {
		String fireFox="Mozilla/5.0 (Linux; U; Android 4.0.4; zh-cn; MI 1S Build/IMM76D) AppleWebKit/530.17 (KHTML, like Gecko) FlyFlow/2.3 Version/4.0 Mobile Safari/530.17 baidubrowser/042_1.6.3.2_diordna_458_084/imoaiX_51_4.0.4_S1+IM/1000213l/108B61C9C1F8457DF2FEF4E662218998%7C816593410747768/1";
		String opera  ="Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B206 MicroMessenger/4.3.2";
		String other  ="Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10A403";
		String windows = "Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0";
		OSBaseTool b=new OSBaseTool();
		System.out.println(b.checkBrowse(fireFox));
		System.out.println(b.checkBrowse(opera));
		System.out.println(b.checkBrowse(other));
		System.out.println(b.checkBrowse(windows));
//		System.out.println(b.check(fireFox,b.checkBrowse(fireFox)));
//		System.out.println(b.check(opera,b.checkBrowse(opera)));
//		System.out.println(b.check(other,b.checkBrowse(other)));
//		System.out.println(b.check(windows,b.checkBrowse(windows)));
	}
	
	//废弃
	public String check(String windows,String OS){
		windows = windows.substring(windows.lastIndexOf(OS));
		windows = windows.substring(0, windows.indexOf(";"));
		windows = windows.substring((OS).length());
//		System.out.println(windows);
		return windows;
	}
	
}
